﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace PloobsEngine.SceneControl
{
    public class ScatterPostEffect : IPostEffect
    {
        bool isDebug = true;
        Effect effect = null;        
        EngineStuff engine;
        float density = 0.1f;
        Vector3 lightPosition = new Vector3(2000.0f, 2800.0f, 2000.0f);
        Effect effect2 = null;        
        RenderTarget2D tr2d;

        public Vector3 LightPosition
        {
            get { return lightPosition; }
            set { lightPosition = value; }
        }

        /// <summary>
        /// Default 0.1
        /// </summary>
        public float Density
        {
            get { return density; }
            set { density = value; }
        }
        float weight = 0.1f;

        /// <summary>
        /// Defautl 0.1
        /// </summary>
        public float Weight
        {
            get { return weight; }
            set { weight = value; }
        }
        float decay = 1.0f;

        /// <summary>
        /// Default 1;
        /// </summary>
        public float Decay
        {
            get { return decay; }
            set { decay = value; }
        }
        float exposition = 0.5f;

        /// <summary>
        /// Default 0.5f
        /// </summary>
        public float Exposition
        {
            get { return exposition; }
            set { exposition = value; }
        }
        float numSamples = 64f;

        /// <summary>
        /// Default 64
        /// </summary>
        public float NumSamples
        {
            get { return numSamples; }
            set { numSamples = value; }
        }

        public void Draw(IRenderHelper rHelper,  Microsoft.Xna.Framework.GameTime gt, Microsoft.Xna.Framework.Rectangle screenRec, IWorld world)
        {
            if(isDebug == true)
            {
            if (Keyboard.GetState().IsKeyDown(Keys.U))
            {
                density -= 0.01f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.I))
            {
                density += 0.01f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.O))
            {
                decay -= 0.01f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.P))
            {
                decay  += 0.01f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.H))
            {
                weight -= 0.01f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.J))
            {
                weight += 0.01f;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.K))
            {
                exposition -= 0.01f;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.L))
            {
                exposition += 0.01f;
            }

        }

            RenderTarget2D x = (RenderTarget2D)EngineStuff.GraphicsDevice.GetRenderTarget(0);
            EngineStuff.GraphicsDevice.SetRenderTarget(0, tr2d);
            effect2.Parameters["EXTRA"].SetValue(rHelper[PrincipalConstants.extra1RT]);
            effect2.Parameters["color"].SetValue(rHelper[PrincipalConstants.CurrentImage]);
            effect2.Parameters["halfPixel"].SetValue(EngineStuff.HalfPixel);
            rHelper.RenderTextureToFullScreenVertexPixel(effect2);
            EngineStuff.GraphicsDevice.SetRenderTarget(0, x);

             effect.Parameters["View"].SetValue(world.ActiveCamera.View);             
             effect.Parameters["WorldViewProjection"].SetValue(world.ActiveCamera.View * world.ActiveCamera.Projection);
             effect.Parameters["LightPosition"].SetValue(lightPosition);
             effect.Parameters["CameraPos"].SetValue(world.ActiveCamera.Position);
             effect.Parameters["numSamples"].SetValue(numSamples);
             effect.Parameters["halfPixel"].SetValue(EngineStuff.HalfPixel);
             effect.Parameters["pb"].SetValue(tr2d.GetTexture());
             effect.Parameters["normal"].SetValue(rHelper[PrincipalConstants.CurrentImage]);
             effect.Parameters["Density"].SetValue(density);
             effect.Parameters["Weight"].SetValue(weight);
             effect.Parameters["Decay"].SetValue(decay);
             effect.Parameters["Exposition"].SetValue(exposition);             
             rHelper.RenderTextureToFullScreenVertexPixel(effect);                      
        }

        public void init(EngineStuff engine)
        {
            effect = EngineStuff.InternalContentManager.GetAsset<Effect>("Scatter");
            effect2 = EngineStuff.InternalContentManager.GetAsset<Effect>("ToBlack");
            this.engine = engine;
            //tr2d = new RenderTarget2D(EngineStuff.GraphicsDevice, EngineStuff.BackBufferWidth, EngineStuff.BackBufferHeight, 0, SurfaceFormat.Color, RenderTargetUsage.PreserveContents);
            tr2d = EngineStuff.GetDefaultColorBuffer();
            this.engine = engine;            

        }

    }
}


